/**
 * Created by Shiun on 2018/5/9.
 */

$(function () {
    var picker = new mui.PopPicker({layer:3});

    picker.setData(cityData);

    $('#selectCity').on('tap',function(){
        picker.show(function (selectItems) {
            //selectItems[0].text
            $('#selectCity').val(selectItems[0].text+selectItems[1].text+selectItems[2].text)
        });
    })
    
    $('#confirm').on('click', function () {
        var username = $.trim($('[name="username"]').val());
        var postcode = $.trim($('[name="postcode"]').val());
        var selectCity = $.trim($('[name="selectCity"]').val());
        var address = $.trim($('[name="address"]').val());

        if(!username){
            mui.toast("请输入收货人姓名");
            return;
        }
        if(!postcode){
            mui.toast("请输入邮编");
            return;
        }
        if(!selectCity){
            mui.toast("请输入城市信息");
            return;
        }
        if(!address){
            mui.toast("请输入地址");
            return;
        }
    
        $.ajax({
            url:'/address/addAddress',
            type:'post',
            data:{
                address: selectCity,
                addressDetail: address,
                recipients: username,
                postcode: postcode
            },
            success:function(res){
                if(res.success){
                    mui.toast("地址添加成功");
                    setTimeout(function () {
                        location.href = "address.html";
                    },1000)
                }
            }

        })

    })

    if(localStorage.getItem("editAddress")){
        var address = JSON.parse(localStorage.getItem("editAddress"));
        console.log(address);

        var html = template('editTpl',address);
        console.log(html);
        $('#edit-box').html(html);
    }
})
